<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取变量的类型</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.get-resource-type.html">« get_resource_type</a></li>
      <li style="float: right;"><a href="function.intval.html">intval »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>获取变量的类型</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.gettype" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">gettype</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">gettype</span> &mdash; <span class="dc-title">获取变量的类型</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.gettype-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>gettype</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   返回 PHP <code class="parameter">value</code> 变量的类型。
   对于类型检查，请使用 <code class="literal">is_*</code> 函数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.gettype-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       要检查类型的变量。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.gettype-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回字符串，可能值为：
   <ul class="simplelist">
    <li class="member">
      <code class="literal">&quot;boolean&quot;</code>
     </li>
    <li class="member">
     <code class="literal">&quot;integer&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;double&quot;</code>
     （由于历史原因，如果是浮点型，则返回 <code class="literal">&quot;double&quot;</code>，而不仅仅是 <code class="literal">&quot;float&quot;</code>）
    </li>
    <li class="member">
     <code class="literal">&quot;string&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;array&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;object&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;resource&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;resource (closed)&quot;</code> 自 PHP 7.2.0 起
    </li>
    <li class="member">
     <code class="literal">&quot;NULL&quot;</code>
    </li>
    <li class="member">
     <code class="literal">&quot;unknown type&quot;</code>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.gettype-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        现在，已关闭的资源报告为 <code class="literal">&#039;resource (closed)&#039;</code>。此前，已关闭的资源报告为
        <code class="literal">&#039;unknown type&#039;</code>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.gettype-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5279">
    <p><strong>示例 #1 <span class="function"><strong>gettype()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$data&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1.</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
integer
double
NULL
object
string
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.gettype-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.get-debug-type.html" class="function" rel="rdfs-seeAlso">get_debug_type()</a> - Gets the type name of a variable in a way that is suitable for debugging</span></li>
    <li class="member"><span class="function"><a href="function.settype.html" class="function" rel="rdfs-seeAlso">settype()</a> - 设置变量的类型</span></li>
    <li class="member"><span class="function"><a href="function.get-class.html" class="function" rel="rdfs-seeAlso">get_class()</a> - 返回对象的类名</span></li>
    <li class="member"><span class="function"><a href="function.is-array.html" class="function" rel="rdfs-seeAlso">is_array()</a> - 检测变量是否是数组</span></li>
    <li class="member"><span class="function"><a href="function.is-bool.html" class="function" rel="rdfs-seeAlso">is_bool()</a> - 检测变量是否是布尔值</span></li>
    <li class="member"><span class="function"><a href="function.is-callable.html" class="function" rel="rdfs-seeAlso">is_callable()</a> - 验证值是否可以在当前范围内作为函数调用。</span></li>
    <li class="member"><span class="function"><a href="function.is-float.html" class="function" rel="rdfs-seeAlso">is_float()</a> - 查找变量的类型是否是浮点型</span></li>
    <li class="member"><span class="function"><a href="function.is-int.html" class="function" rel="rdfs-seeAlso">is_int()</a> - 检测变量是否是整数</span></li>
    <li class="member"><span class="function"><a href="function.is-null.html" class="function" rel="rdfs-seeAlso">is_null()</a> - 检测变量是否是 null</span></li>
    <li class="member"><span class="function"><a href="function.is-numeric.html" class="function" rel="rdfs-seeAlso">is_numeric()</a> - 检测变量是否为数字或数字字符串</span></li>
    <li class="member"><span class="function"><a href="function.is-object.html" class="function" rel="rdfs-seeAlso">is_object()</a> - 查找变量是否是字符串</span></li>
    <li class="member"><span class="function"><a href="function.is-resource.html" class="function" rel="rdfs-seeAlso">is_resource()</a> - 查找变量是否为资源</span></li>
    <li class="member"><span class="function"><a href="function.is-scalar.html" class="function" rel="rdfs-seeAlso">is_scalar()</a> - 检测变量是否是一个标量</span></li>
    <li class="member"><span class="function"><a href="function.is-string.html" class="function" rel="rdfs-seeAlso">is_string()</a> - 查找变量的类型是否是字符串</span></li>
    <li class="member"><span class="function"><a href="function.function-exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - 如果给定的函数已经被定义就返回 true</span></li>
    <li class="member"><span class="function"><a href="function.method-exists.html" class="function" rel="rdfs-seeAlso">method_exists()</a> - 检查类的方法是否存在</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>